﻿#include <QHeaderView>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QToolButton>
#include <QScrollBar>
#include <QDateTime>
#include "JZLogWidget.h"

JZLogWidget::JZLogWidget(QWidget *parent) 
    :QWidget(parent)
{    
    QHBoxLayout *l = new QHBoxLayout();
    l->setContentsMargins(0, 0, 0, 0);
    
    QVBoxLayout *v = new QVBoxLayout();
    v->setContentsMargins(0, 0, 0, 0);
    v->setSpacing(1);

    QToolButton *btnClear = new QToolButton();
    btnClear->setText("Clear");
    connect(btnClear, &QToolButton::clicked, this, &JZLogWidget::onBtnClearClicked);

    l->addWidget(btnClear);
    l->addStretch();

    m_tableWidget = new QTableWidget();
    v->addLayout(l);
    v->addWidget(m_tableWidget);
    setLayout(v);

    QFont Font;
    Font.setFamily("Microsoft YaHei");
    Font.setPixelSize(15 * 1);
    Font.setBold(false);
    m_tableWidget->horizontalHeader()->setFont(Font);

    m_levelList << tr("DEBUG") << tr("INFO") << tr("WARN") << tr("ERROR") << tr("FATAL");

    QStringList strListHeader;
    strListHeader << tr("Time") << tr("Module") << tr("Level") << tr("Message");
    m_tableWidget->clear();
    m_tableWidget->setColumnCount(4);    

    m_tableWidget->setHorizontalHeaderLabels(strListHeader);
    m_tableWidget->verticalHeader()->setVisible(false);

    m_tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);  
    m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_tableWidget->horizontalHeader()->setStretchLastSection(true);    
    m_tableWidget->setEditTriggers(QTableWidget::NoEditTriggers);      

    JZLogManager::instance()->addObserver(LogModule_General, this);
}

JZLogWidget::~JZLogWidget()
{
}


void JZLogWidget::addLog(JZLogObjectPtr obj)
{    
    m_logList.push_back(obj);
    if (m_logList.size() > 1000)
    {
        m_logList.remove(0, 500);
        for (int i = 0; i < 500; i++)
            m_tableWidget->removeRow(0);
    }    
    addLogItem(obj.data());    
}

void JZLogWidget::addLogItem(JZLogObject *obj)
{        
    QTableWidgetItem *item0 = new QTableWidgetItem();
    QString time_str = QDateTime::fromMSecsSinceEpoch(obj->time).toString("HH:mm:ss");
    item0->setText(time_str);

    QTableWidgetItem *item1 = new QTableWidgetItem();
    item1->setText(JZLogManager::instance()->moduleName(obj->module));

    QTableWidgetItem *item2 = new QTableWidgetItem();
    item2->setText(m_levelList[obj->level]);

    QTableWidgetItem *item3 = new QTableWidgetItem();
    item3->setText(obj->message);

    int n = m_tableWidget->rowCount();
    if (n > 999)
    {
        m_tableWidget->removeRow(0);
        n = n - 1;
    }

    m_tableWidget->setRowCount(n + 1);
    m_tableWidget->setItem(n, 0, item0);
    m_tableWidget->setItem(n, 1, item1);
    m_tableWidget->setItem(n, 2, item2);
    m_tableWidget->setItem(n, 3, item3);

    QFont Font;
    Font.setFamily("Microsoft YaHei");
    Font.setPixelSize(15 * 1);
    Font.setBold(false);

    item0->setFont(Font);
    item1->setFont(Font);
    item2->setFont(Font);
    item3->setFont(Font);

    m_tableWidget->verticalScrollBar()->setValue(m_tableWidget->verticalScrollBar()->maximum());
}

void JZLogWidget::updateLog()
{
    m_tableWidget->clearContents();
    m_tableWidget->setRowCount(0);

    for (int i = 0; i < m_logList.size(); i++)
        addLogItem(m_logList[i].data());
}

void JZLogWidget::onBtnSettingClicked()
{
}

void JZLogWidget::onBtnClearClicked()
{
    m_logList.clear();
    updateLog();
}